package com.nfcpay;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;

import org.bouncycastle.openssl.PEMDecryptorProvider;
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
import org.bouncycastle.openssl.PEMException;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;

public class AssinaturaUtils {
	
	private String recibo;
	
	public static void main() {
		
	}
	
	public byte[] assinar(String recibo, File file){
		this.recibo = recibo;
		
		//TODO verificar como acessa um arquivo
		String prvtKeyFileName = "nfcPay/res/raw/privatekey.der";
		String password = "1234key";
		KeyPair kp = getKeyPair(file, password);
		
		try {
			Signature sign = Signature.getInstance("SHA1WithRSA");
			sign.initSign(kp.getPrivate());
			sign.update(recibo.getBytes());
			byte[] signatureBytes = sign.sign();
			return signatureBytes;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SignatureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
//		KeyFactory kf = KeyFactory.getInstance("RSA");
//		
//		javax.crypto.EncryptedPrivateKeyInfo arg0 = new javax.crypto.EncryptedPrivateKeyInfo(encoded);
//		Key decryptKey = X509;
//		
//		PEMParser pemParser = new PEMParser(arg01);
//		
////		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encodedKey);
////		x509KeySpec.
//		PKCS8EncodedKeySpec keySpec = arg0.getKeySpec(decryptKey );
//		PKCS8EncryptedPrivateKeyInfo a = new PKCS8EncryptedPrivateKeyInfo(arg0);
//		PrivateKey privateKey = kf.generatePrivate(x509KeySpec);
	}
	
	public KeyPair getKeyPair(File prvtKeyFile, String password) {
		try {
			File privateKeyFile = prvtKeyFile; // private key file in PEM format
			PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile));
			Object object = pemParser.readObject();
			PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(password.toCharArray());
			JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
			KeyPair kp;
			if (object instanceof PEMEncryptedKeyPair) {
			   System.out.println("Encrypted key - we will use provided password");
			   kp = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv));
			   return kp;
			} else {
			   System.out.println("Unencrypted key - no password needed");
			   kp = converter.getKeyPair((PEMKeyPair) object);
			   return kp;
			}
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			return null;
		} catch (PEMException e) {
			// TODO: handle exception
			return null;
		} catch (IOException e) {
			// TODO: handle exception
			return null;
		}
	}

	//TODO possivilidade de ser privado
	public String getRecibo() {
		return recibo;
	}

	public void setRecibo(String recibo) {
		this.recibo = recibo;
	}
	
	
}
